package org.example.mybatisplus.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.example.mybatisplus.bean.Emp;
import org.example.mybatisplus.service.EmpService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;



@SpringBootTest
@Slf4j
public class App {
    @Resource
    private EmpService service;
    @Test
    public void find(){
        List<Emp> list = service.list();
        list.forEach(e->{
            log.info("e:{}",e);
        });
    }
    @Test
    public void finById(){
        Emp emp = service.getById(8040);
        log.info("emp:{}",emp);
    }
    @Test
    public void save(){
        Emp emp = new Emp();
        emp.setComm(8532.5);
        emp.setJob("程序员");
        emp.setEname("xx");
        emp.setMgr(8989);
        emp.setSal(30000.8);
        emp.setDeptNo(30);
        service.save(emp);
    }
    @Test
    public void update(){
        Emp emp = new Emp();
        emp.setJob("高手");
        emp.setSal(30000.8);
        emp.setEmpNo(8034);
        service.updateById(emp);
    }
    @Test
    public void delete(){
        service.removeById(1292369923);
    }
    @Test
    public void find1(){
       //按照职位进行查询 where job="?"
        //1.产生wrapper对象
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.eq("job","程序员");//job=程序员
        //3.查询
        List<Emp> list = service.list(wrapper);
        list.forEach(System.out::println);
    }
    @Test
    public void find2(){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // =是eq <>是ne >是gt >=是ge <是lt <=是le
        wrapper.gt("sal",8000);
        List<Emp> list = service.list(wrapper);
        list.forEach(System.out::println);
    }
    @Test
    public void find3(){
        //模糊查询
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        //wrapper.like("ename","张");//%张%
        //wrapper.likeRight("ename","张");//张%
        // wrapper.likeLeft("ename","张");//%张
        wrapper.notLike("ename","张");
        service.list(wrapper);
    }
    @Test
    public void find4(){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        //wrapper.orderByAsc("sal");
        wrapper.orderByDesc("sal");
        service.list(wrapper);
    }
    @Test
    public void find5(){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        //wrapper.select("job","ename");
        wrapper.select("count(0),max(sal),min(sal),avg(sal),sum(sal)");
        service.list(wrapper);

    }
    @Test
    public void find6(){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.groupBy("job");
        wrapper.select("job","count(0)");
        service.list(wrapper);
    }
    @Test
    public void find7(){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.last("and job='程序员'");
        service.list(wrapper);
    }
    @Test
    public void find8(){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        //如果写入多个条件 是and
        wrapper.eq("job","程序员");//=
        wrapper.lt("sal",30000);//<
        wrapper.lt("comm",888);//>
        service.list(wrapper);
    }
    @Test
    public void find9(){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        //如果写入多个条件用 or
        wrapper.eq("job","程序员")
               .or().lt("sal",30000)
                .or().gt("comm",888);
        service.list(wrapper);
    }
    @Test
    public void find10(){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        //如果写入多个条件 复杂情况
        wrapper.eq("job","程序员");
        wrapper.lt("sal",30000)
                .or().gt("comm",888);
        service.list(wrapper);

    }
    @Test
    public void update1(){
            UpdateWrapper<Emp> wrapper = new UpdateWrapper<>();
        wrapper.eq("ename","李三思");//改谁
        wrapper.set("ename","马三四");//改成什么
        service.update(wrapper);
    }
    @Test
    public void update2(){
        ArrayList<Emp> list = new ArrayList<>();
        Emp emp = new Emp();
        emp.setSal(3388.0);
        emp.setEmpNo(7906);
        list.add(emp);
        Emp emp1 = new Emp();
        emp1.setSal(4388.0);
        emp1.setEmpNo(7907);
        list.add(emp1);
        Emp emp2 = new Emp();
        emp.setSal(99.6);
        emp.setEmpNo(7908);
        list.add(emp2);
        service.piliangcaozuo(list);

    }
}


